/**
 * Created by liweizhao on 2017/4/27.
 */
var i = 0;
var id = getQueryString('id');
window.onload = function () {
    $.get('/article/getArticle/', {id: id}, function (data) {
        if (data.status) {
            $('.title').html(data.info.title);
            $('#artTime').html(data.info.cd);
            $('#like').html('赞 : ' + data.info.like_num);
            $('#read').html('阅 : ' + data.info.browse_num);
//                $('#label').parent('div').append(data.info.content);
            $('#artContent').html(data.info.content);
            if (data.info.label) {
                $.get("/article/getlabel/", {label: data.info.label}, function (info) {
                    if (info.status) {
                        var tags = '';
                        $.each(info.info, function (k, v) {
                            tags = tags + '<li>' +
                                '<a href="' + v.id + '">' + v.name + '</a>' +
                                '</li>';
                        });
                        $('.tags').html(tags);
                    } else {
                        alert(info.info);
                    }
                });
            } else {
                return false;
            }
        } else {
            alert(data.info);
        }

    });
    //动态加载评论
    var j = 1;
var timer = setInterval(function () {
    var mesBottom = parseFloat($('#mesDiv').height() + $('#mesDiv').offset().top);
    var viewBottom = parseFloat($(document).scrollTop() + $(window).height());
    if ((mesBottom - viewBottom) <= 0) {
        $.get('/article/getmes', {page: j, id: id}, function (mesDet) {
            if (mesDet.status) {
                j++;
                var html = '';
                $.each(mesDet.info, function (k, v) {
                    html = html + '<div class="row first-level" style="margin-bottom:30px;">' +

                        '<div class="col-md-10 col-sm-9 col-xs-7">' +
                        '<div class="row">' +
                        '<span class="username">' +
                        '<a href="#">' +
                        '<strong>' + v.name + '</strong>' +
                        '</a>' +
                        '</span>' +

                        '<p class="publish-date">' + v.cd + '</p>' +
                        '</div>' +
                        '<div class="row">' +
                        '<p>' +
                        v.content +
                        '</p>' +
                        '</div>' +
                        '</div>' +
                        '</div>';
                });
                $('#mesDiv').append(html);
            } else {
                var getInfo = '<button class="btu">' + mesDet.info + '</button>';
                $('#mesDiv').append(getInfo);
                clearTimeout(timer);
                return false;
            }
        });
    }

}, "1000");
};



//提交评论
function sbumitForm() {
    var name = $('#comment_author').val();
    var email = $('#comment_email').val();
    var message = $('#comment_content').val();
    if (name) {
        var nameInfo = true;
    } else {
        var nameInfo = false;
        alert('留言的话请先留下大名!');
        return false;
    }
    if (i == 0) {
        if (email) {
            var emailInfo = true;
        } else {
            i = 1;
            alert('留下邮箱方便交流,实在不留也拦不住你');
            return false;
        }
    } else {
        var emailInfo = true;
    }
    if (message) {
        var mesInfo = true;
    } else {
        var mesInfo = false;
        alert('留言内容不填,可以理解为想撩博主吗?');
        return false;
    }
    if (nameInfo && emailInfo && mesInfo) {

        if (CheckMail(email) == 'rightmail') {
            if (name.length <= 10) {
                $.post("/article/mescreat", {
                    name: name,
                    content: message,
                    email: email,
                    id: id
                }, function (mes) {
                    if (mes.status) {
                        var html = '<div class="row first-level" style="margin-bottom:30px;">' +

                            '<div class="col-md-10 col-sm-9 col-xs-7">' +
                            '<div class="row">' +
                            '<span class="username">' +
                            '<a href="#">' +
                            '<strong>' + mes.info.name + '</strong>' +
                            '</a>' +
                            '</span>' +

                            '<p class="publish-date">' + mes.info.cd + '</p>' +
                            '</div>' +
                            '<div class="row">' +
                            '<p>' +
                            mes.info.content +
                            '</p>' +
                            '</div>' +
                            '</div>' +
                            '</div>';
                        $('#mesDiv').prepend(html);
                    } else {
                        alert(mes.info);
                    }
                });
            } else {
                alert('名字的长度不要超过10个字');
                return false;
            }
        } else {
            alert('邮箱都填了,至少格式写个正确的吧');
            return false;
        }
    } else {
        return false;
    }

}

//点赞文章
function likeArticle() {
$.get('likearticle.html',{id:id},function (data) {
    if(data.status){
        alert(data.info);
        $('#plusone').show();
    }else {
        alert(data.info);
    }
});
}
//检查Email格式
function CheckMail(mail) {
    if (mail === '') {
        return false;
    } else {
        var filter_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var filter_mob = /^0?1[3|4|5|8][0-9]\d{8}$/;
        if (filter_mail.test(mail)) {
            return "rightmail";
        } else {
            if (filter_mob.test(mail)) {
                return "rightmob";
            } else {
                return false;
            }
        }
    }
}


